import { flow, makeAutoObservable, } from "mobx";
import { request } from "../../utils/request";
import Loading from "../../components/widget/Loading";

export default class MineStore {
    info: any = {};
    noteList: ArticleSimple[] = []
    collectionList: ArticleSimple[] = []
    favorateList: ArticleSimple[] = []

    refreshing: boolean = false
    constructor() {
        // 在构造函数中，我们使用makeObservable来定义可观察属性和动作
        makeAutoObservable(this);
    }

    requestAll = async () => {
        Loading.show()
        this.refreshing = true
        Promise.all([
            this.requestInfo(),
            this.requestNoteList(),
            this.requestCollectionList(),
            this.requestFavorateList(),

        ]).then(() => {
            Loading.hide()
            this.refreshing = false
        })
    }
    requestNoteList = flow(function* (this: MineStore) {
        try {
            const { data } = yield request('noteList', {});
            this.noteList = data || [];
        } catch (e) {
            console.error(e);
        }
    }).bind(this);
    requestCollectionList = flow(function* (this: MineStore) {
        try {
            const { data } = yield request('collectionList', {});
            this.collectionList = data || []
        } catch (e) {
            console.error(e);
        }
    }).bind(this);
    requestFavorateList = flow(function* (this: MineStore) {
        try {
            const { data } = yield request('favorateList', {});
            this.favorateList = data || [];
        } catch (e) {
            console.error(e);
        }
    }).bind(this);
    requestInfo = flow(function* (this: MineStore) {
        try {
            const { data } = yield request('accountInfo', {});
            this.info = data || {};
        } catch (e) {
            console.error(e);
        }
    }).bind(this);
    // 由于flow返回的是一个函数，需要手动绑定this上下文

    // requestInfo = async () => {
    //     try {
    //         const { data } = await request('accountInfo', {});
    //         this.info = data || {};
    //     } catch (e) {
    //         console.error(e);
    //     }
    // }
}